<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <DetailModal @register="register" />
    <MchDrawer @register="registerMchDrawer" @success="reload" />
  </div>
</template>
<script lang="ts" name="PayIfDefine" setup>
  import { ref } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import DetailModal from "./DetailModal.vue"
  import MchDrawer from "./PayIfDefineDrawer.vue"
  import { useGlobSetting } from "/@/hooks/setting"
  import { columns, searchFormSchema } from "./payIfDefine.data"
  import { listPayIfDefine, deletePayIfDefine } from "/@/api/pay"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { useRoute, useRouter } from "vue-router"
  const { createMessage } = useMessage()
  const router = useRouter()

  const glob = useGlobSetting()
  const [register, { openDrawer: openDetail }] = useDrawer()
  const [registerMchDrawer, { openDrawer: openMchDrawer }] = useDrawer()
  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: "PayIfDefine",
    tableProps: {
      title: "支付接口定义",
      api: listPayIfDefine,
      columns: columns,
      bordered: false,
      actionColumn: {
        width: 150,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
      formConfig: {
        schemas: searchFormSchema,
        fieldMapToTime: [["fieldTime", ["beginDate", "endDate"], "YYYY-MM-DD HH:mm:ss"]],
      },
    },
  })

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext

  /**
   * 查看
   */
  function handleDetail(record) {
    openDetail(true, {
      record,
      isUpdate: true,
    })
  }
  /**
   * 编辑
   */
  function handleEdit(record: Recordable) {
    openMchDrawer(true, {
      record,
      isUpdate: true,
    })
  }

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: "查看",
        onClick: handleDetail.bind(null, record),
        // ifShow: record.sendStatus == 0
      },
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
        // ifShow: record.sendStatus == 0
      },
      {
        label: "删除",
        color: "error",
        ifShow: record.sendStatus != 1,
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }

  /**
   * 删除
   */
  const handleDelete = async (record) => {
    await deletePayIfDefine({ id: record.id }, () => {
      reload()
      createMessage.success("删除成功")
    })
  }

  /**
   * 新增
   */
  function handleCreate() {
    openMchDrawer(true, {
      isUpdate: false,
    })
  }
</script>
